package com.neu.architecture.service.impl;


import com.github.pagehelper.PageHelper;
import com.neu.architecture.dto.InfoParam;
import com.neu.architecture.dto.InfoQueryParam;
import com.neu.architecture.mapper.InfoMapper;
import com.neu.architecture.model.Info;
import com.neu.architecture.model.InfoExample;
import com.neu.architecture.service.ArchitectureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ArchitectureServiceImpl implements ArchitectureService {

    @Autowired
    private InfoMapper infoMapper;  // 这个报错没问题的

    @Override
    public List<Info> fetchList(Integer pageSize, Integer pageNum) {
        // pageHelper 设置
        PageHelper.startPage(pageNum, pageSize);
        // 参数为空 example, 相当于查询所有数据
        InfoExample infoExample = new InfoExample();
        List<Info> infos = infoMapper.selectByExample(infoExample);
        return infos;
    }

    @Override
    public List<Info> searchArchis(Integer pageSize, Integer pageNum, InfoQueryParam param) {
        PageHelper.startPage(pageNum, pageSize);
        InfoExample infoExample = new InfoExample();
        InfoExample.Criteria criteria = infoExample.createCriteria();
        if (param.getName() != null && !param.getName().equals("")) {
            criteria.andNameLike("%" + param.getName() + "%");
        }
        if (param.getCity() != null && !param.getCity().equals("")) {
            criteria.andCityLike("%" + param.getCity() + "%");
        }
        if (param.getLargeCate() != null && !param.getLargeCate().equals("")) {
            criteria.andLargeCateEqualTo(param.getLargeCate());
        }
        if (param.getMidCate() != null && !param.getMidCate().equals("")) {
            criteria.andMidCateEqualTo(param.getMidCate());
        }
        if (param.getMinCate() != null && !param.getMinCate().equals("")) {
            criteria.andMinCateEqualTo(param.getMinCate());
        }
        return infoMapper.selectByExample(infoExample);
    }

    @Override
    public int deleteArchi(long id) {
        return infoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Info> getArchiById(long id) {
        return List.of(infoMapper.selectByPrimaryKey(id));
    }

    @Override
    public int updateArchi(InfoParam param, long id) {
        Info info = new Info();
        BeanUtils.copyProperties(param, info);
        info.setId(id);
        return infoMapper.updateByPrimaryKeySelective(info);
    }

    @Override
    public int addArchi(Info info) {
        return infoMapper.insert(info);
    }

}
